<?php
namespace ChannelThrift\Service;

use Base\BaseService;

class SceneService extends BaseService {
    Protected $autoCheckFields = false;
    public  $connection = 'DB_AUMS';
    public $tablePrefix = 'au_';

    /**
     * 获取场信息
     * @param $map
     * @return bool|mixed
     */
    public function getSceneInfo($map)
    {
        $scene =  D("ChannelThrift/Aums/Scene")->where($map)->find();
        return $scene;
    }

    /**
     * 根据条件获取所有的场
     * @param $map
     * @return mixed
     */
    function getScenes($map)
    {
        $scenes =  D("ChannelThrift/Aums/Scene")->where($map)->select();
        return $scenes;
    }


    /**
     * 新增场信息
     * @param $data
     * @return bool|mixed
     */
    public function addScene($data)
    {
        if(empty($data)){
            return false;
        }
        $data['createtime'] = date('Y-m-d H:i:s');
        $scene_id =  D("ChannelThrift/Aums/Scene")->add($data);
        return $scene_id;
    }

    /**
     * 修改场信息
     * @param $data
     * @return bool|mixed
     */
    public function editScene($map,$data)
    {
        if(empty($map) || empty($data) ){
            return false;
        }
        $scene_id =  D("ChannelThrift/Aums/Scene")->where($map)->save($data);
        return $scene_id;
    }

    /**
     * 判断活动是否有效
     *
     * @param int $activityId 活动编号
     * @param string $time 活动时间
     * @return mixed
     */
    public function isActivityAvailable($activityId, $time){
        $map['starttime'] = ['elt',$time];
        $map['endtime'] = ['egt',$time];
        $map['id'] = ['eq',$activityId];
        $result = D("ChannelThrift/Aums/SceneActivity")->where($map)->find();
        return $result;
    }

    /**
     * 检查场次开始时间是否在定时单前面
     * @param $scene_id
     * @param $starttime
     * @return bool
     */
    public function checkStarttime($sceneId,$starttime){
        $res = D('ChannelThrift/Aums/Order')->where("scene_id='".$sceneId."' and is_timing_order=1 and rank>0 and status not in(2,6,12)")->order('bidding_start_time')->find();
        if($res){
            if(strtotime($res['bidding_start_time'])<$starttime){
                return false;
            }
        }
        return true;
    }

    /**
     * 添加或者编辑场次活动
     *
     * @param array $data 场次活动关系数据
     * @return bool|mixed
     */
    public function addOrEditeSceneActivity($data){
        $map = array();
        $map['scene_id'] = ['eq',$data['scene_id']];
        $m = D('ChannelThrift/Aums/SceneActivityRelation');
        $obj = $m->where($map)->find();
        if($obj){
            $result = $m->where($map)->save($data);
            return $result;
        }else{
            $result = $m->add($data);
            return $result;
        }
    }

    /**
     * 修改场需要重新计算场次内非定时单的开拍时间
     * @param $sceneId
     * @return bool
     */
    public function reComputeStarttime($sceneId){
        $res = D('ChannelThrift/Aums/Scene')->alias('os')->field('os.starttime,o.order_id,o.act_elapsed_time,o.est_elapsed_time')->join(C('DB_AUMS.DB_PREFIX')."order o ON o.scene_id=os.scene_id")
            ->where("os.scene_id='".$sceneId."' AND o.is_timing_order=0 AND o.rank>0 AND o.status NOT IN(2,6,12)")->order("o.rank")
            ->select();
        $arr = array();
        $timestamp = 0;
        foreach($res as $v){
            $timestamp += $v['act_elapsed_time']>$v['est_elapsed_time']?$v['act_elapsed_time']:$v['est_elapsed_time'];
            $arr[] = $timestamp;
        }
        //
        foreach($res as $key=>$val){
            $data = array();
            $data['bidding_start_time'] = date("Y-m-d H:i:s",strtotime($val['starttime'])+$arr[$key-1]);
            $data['last_time'] = date('Y-m-d H:i:s',time());
            $rs=D('ChannelThrift/Aums/Order')->where("order_id='".$val['order_id']."'")->save($data);
            if($rs===false){
                return false;
            }
        }
        return true;
    }

    /**
     * 根据场次编号，删除活动
     * @param array $data
     * @return bool
     */
    public function deleteSceneActivity($data){
        $map['scene_id'] = ['eq',$data['scene_id']];
        $m = D('ChannelThrift/Aums/SceneActivityRelation');
        $result = $m->where($map)->delete();
        if($result === false){
            return false;
        }else{
            return true;
        }
    }

}
